<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Categories &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
  <script src="../assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
  <script>NProgress.start()</script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="login.html"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>分类目录</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong>发生XXX错误
      </div> -->
      <div class="row">
        <div class="col-md-4" id="modifyBox">
          <form id="additem">
            <h2>添加分类</h2>
            <div class="form-group">
              <label>名称</label>
              <input class="form-control" type="text" placeholder="请输入分类名称" name="title">
            </div>
            <div class="form-group">
              <label>图标</label>
              <input class="form-control" type="text" placeholder="请输入分类图标类名" name="className">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
        </div>
        <div class="col-md-8">
          <!-- <div class="page-action">
            <a class="btn btn-danger btn-sm" href="javascript:;">批量删除</a>
          </div> -->
          <table class="table table-striped table-bordered table-hover">
            <thead>
              <tr>
                <th class="text-center" width="40"><input type="checkbox"></th>
                <th>名称</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody id="addbox">

            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="../uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li class="active">
        <a href="#menu-posts" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse in">
          <li><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li class="active"><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li>
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li>
        <a href="#menu-settings" class="collapsed" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse">
          <li><a href="slides.html">图片轮播</a></li>
          <li><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>
  <script src="../assets/vendors/jquery/jquery.js"></script>
  <script src="../assets/vendors/bootstrap/js/bootstrap.js"></script>
  <!-- 引入模板文件 -->
  <script src="../assets/vendors/art-template/template-web.js"></script>
  <script>NProgress.done()</script>
  <!-- 定义添加分类模板 -->
  <script type="text/html" id="addTpl">
    {{each data}}
    <tr>
        <td class="text-center"><input type="checkbox"></td>
        <td>{{$value.title}}</td>
        <td class="text-center">
          <a href="javascript:;" data-id = "{{$value._id}}" class="btn btn-info btn-xs edit">编辑</a>
          <a href="javascript:;" data-id = "{{$value._id}}" class="btn btn-danger btn-xs delete">删除</a>
        </td>
      </tr>
      {{/each}}
  </script>
  <!-- 定义修改分类模板 -->
  <script type="text/html" id="modifyTpl">
    <form id="modifyitem" data-id = "{{_id}}">
      <h2>修改分类</h2>
      <div class="form-group">
        <label>名称</label>
        <input class="form-control" type="text" placeholder="请输入分类名称" name="title" value="{{title}}">
      </div>
      <div class="form-group">
        <label>图标</label>
        <input class="form-control" type="text" placeholder="请输入分类图标类名" name="className" value="className">
      </div>
      <div class="form-group">
        <button class="btn btn-primary" type="submit">保存</button>
      </div>
    </form>
  </script>
  <script>
    $(function () {
      //添加分类功能
      //为表单注册submit提交事件
      $('#additem').on('submit', function () {
        //获取用户输入的所有表单信息
        var formData = $(this).serialize();
        //发送ajax请求，实现分类添加功能
        $.ajax({
          type: 'post',
          url: 'http://47.111.184.55:3000/categories',
          data: formData,
          success: function (response) {
            // console.log(response);
            //重新加载渲染页面
            location.reload();
          }
        });
        //阻止表单的默认提交行为
        return false;
      });
      //分类列表展示
      //发送ajax请求，获取后台数据渲染页面
      $.ajax({
        type: 'get',
        url: 'http://47.111.184.55:3000/categories',
        success: function (response) {
          // console.log(response)
          var html = template('addTpl', {
            data: response,
          });
          $('#addbox').html(html);
        }
      });

      //分类信息编辑操作
      //利用事件委托为编辑按钮注册点击事件
      $('#addbox').on('click', '.edit', function () {
        var id = $(this).attr('data-id');
        $.ajax({
          type: 'get',
          url: `http://47.111.184.55:3000/categories/${id}`,
          success: function (response) {
            var html = template('modifyTpl', response);
            $('#modifyBox').html(html);
          }
        });
      });
      //将数据同步到数据库中
      //利用事件委托为表单注册submi提交事件
      $('#modifyBox').on('submit', '#modifyitem', function () {
        //获取用户修改后的表单所有内容和ID
        var formData = $(this).serialize();
        var id = $(this).attr('data-id');
        // console.log(id);
        //发送ajax请求
        $.ajax({
          type: 'put',
          url: `http://47.111.184.55:3000/categories/${id}`,
          data: formData,
          success: function (response) {
            location.reload();
          }
        });
        //阻止表单的默认行为
        return false;
      });
      //删除分类信息
      //利用事件委托为删除按钮注册点击事件
      $('#addbox').on('click', '.delete', function () {
        //获取当前被点击的分类id
        var id = $(this).attr('data-id');
        var isDelete = confirm('确认要删除吗?');
        if (isDelete) {
          //发送ajax请求
          $.ajax({
            type: 'delete',
            url: `http://47.111.184.55:3000/categories/${id}`,
            success: function (response) {
              location.reload();
            }
          });
        }
      });
      
    });

  </script>
</body>

</html>